//
//  SEGInputBarView.m
//  SEGUhomeSommunitySocial
//
//  Created by 揭婧雯 on 2018/12/3.
//  Copyright © 2018 刘山. All rights reserved.
//

#import "SEGInputBarView.h"
#import "SEGPlatformManager.h"
#import "SEGBasicBussinessCommonDefine.h"

static CGFloat KSEGTextViewHeight = 35.0;

static CGFloat KSEGParkTextViewHeight = 50.0;
static CGFloat KTextViewBottomPadding = 18.0;

@interface SEGInputBarView ()<SEGTextViewDelegate>

@property (nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation SEGInputBarView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = kColor_TABLE;
 
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.top.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.right.equalTo(self);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(50);
    }];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(KSEGTextViewHeight);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self.sendButton.mas_left);
        make.top.equalTo(self).offset(7.5);
    }];
}



#pragma mark - SEGTextViewDelegate
- (void)textViewDidChange:(SEGTextView *)textView
{
    CGFloat maxHeight = 112;
    CGRect frame = textView.frame;
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [textView sizeThatFits:constraintSize];
    CGFloat totalHeight = size.height + 10;
    //限制高度最小值和最大值
    if (totalHeight < KSEGTextViewHeight) {
        totalHeight = KSEGTextViewHeight;
    }
    if (totalHeight > maxHeight) {
        totalHeight = maxHeight;
    }
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(totalHeight+14.5);
    }];
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(totalHeight);
    }];
    [self layoutIfNeeded];
}

#pragma mark - lazy load

- (SEGTextView *)textView {
    if (!_textView) {
        _textView = [[SEGTextView alloc] init];
        _textView.textColor = kColor_1;
        _textView.font = kFont_15;
        _textView.backgroundColor = kColor_W;
        _textView.tintColor = kColor_T;//光标颜色
        _textView.layer.masksToBounds = YES;
        _textView.layer.cornerRadius = 2.5;
        _textView.textContainerInset = UIEdgeInsetsMake(10, 10, -10, -10);
        _textView.placeholderText = @"说说你的观点";
        _textView.placeholderTextColor = kColor_C;
        _textView.placeholderFont = kFont_15;
        _textView.delegate = self;
        _textView.layer.borderColor = kColor_L2.CGColor;
        _textView.layer.borderWidth = 0.5;
        [self addSubview:_textView];
        
    }
    return _textView;
}

- (UIButton *)sendButton {
    if (!_sendButton) {
        _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendButton.titleLabel.font = kFont_16;
        [_sendButton setTitle:@"发送" forState:UIControlStateNormal];
        [_sendButton setTitleColor:kColor_T forState:UIControlStateNormal];
        [self addSubview:_sendButton];
    }
    return _sendButton;
}

- (void)setPlaceholderText:(NSString *)placeholderText
{
    if (placeholderText.length > 0) {
        self.textView.placeholderText = placeholderText;
    }
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    _textView.layer.borderColor = kColor_L2.CGColor;
    _textView.placeholderTextColor = kColor_C;
    _textView.textColor = kColor_1;
}

@end
